package com.parking.stall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.Result;
import com.parking.common.core.util.JwtUtil;
import com.parking.common.web.util.OssUtil;
import com.parking.stall.dto.RentersSelectStallCheckDTO;
import com.parking.stall.exception.StallException;
import com.parking.stall.exceptionCode.StallExceptionCode;
import com.parking.stall.form.*;
import com.parking.stall.param.*;
import com.parking.stall.service.StallCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 彭于晏
 * @since 2023年11月29日
 */
@RestController
@RequestMapping("/stallCheck")
@Api(tags = "车位审核接口（物业、出租客）")
public class StallCheckController {
    /*车位审核*/
    @Autowired
    private StallCheckService stallCheckService;
    @Autowired
    private OssUtil ossUtil;


    /**
     * upload:上传图片
     * @param file 上传图片文件
     * @return url：图片地址
     * @throws Exception
     */
    @ApiOperation("上传图片（车位购买证明）")
    @PostMapping("/upload")//上传图片
    public Result<String> upload(MultipartFile file) throws Exception{
        //获取文件名称
        String filename = file.getOriginalFilename();
        String name = UUID.randomUUID().toString();
        filename = name + filename.substring(filename.lastIndexOf("."));
        String url=ossUtil.uploadFile(file.getInputStream(),filename);
        System.out.println("新增图片地址："+url);
        return Result.getResult(String.class).setMessage("上传成功").setData(url);
    }


    /**
     * delImg:删除图片
     * @param form 删除图片对象
     * @return b true/false
     * @throws Exception
     */
    @PostMapping("/del")
    @ApiOperation("删除图片（车位购买证明）")
    public Result<Boolean> delImg (@RequestBody DelImgForm form)throws Exception{
        System.out.println("删除图片地址："+form);
        String substring = form.getStallBuyProve().substring(form.getStallBuyProve().lastIndexOf("/")+1);
        if (!StringUtils.hasLength(form.getStallBuyProve())) {
            throw new StallException(StallExceptionCode.IMG_NOT_NULL);
        }
        boolean b = ossUtil.deleteFile(substring);
        return Result.getResult(Boolean.class).setMessage("删除成功").setData(b);
    }


    /**
     * add：出租客申请车位入驻物业
     * @param form:出租客车位申请参数类
     * @param authorization:token
     * @return:true/false
     * @throws Exception
     */
    @PostMapping("/add.renters")
    @ApiOperation("出租客申请车位入驻")
    public Result<Boolean> add(@RequestBody @Validated AddStallCheckForm form,@RequestHeader String authorization) throws Exception{
        Integer rentersId = JwtUtil.getRentersId(authorization);
        AddStallCheckParam param = BeanUtil.toBean(form, AddStallCheckParam.class);
        param.setRentersId(rentersId);
        Boolean aBoolean = stallCheckService.add(param);
        return Result.getResult(Boolean.class).setMessage("申请成功").setData(aBoolean);
    }


    /**
     * certifiedCheck:物业审核出租客申请
     * @param form:物业审核出租客申请参数类
     * @param authorization:token
     * @return:true/false
     * @throws Exception
     */
    @PostMapping("/certifiedCheck.certified")
    @ApiOperation("物业审核出租客申请")
    public Result<Boolean> certifiedCheck(@RequestBody CertifiedCheckStallForm form,@RequestHeader String authorization)throws Exception{
        JwtUtil.getCertifiedAccountId(authorization);
        CertifiedCheckStallParam param = BeanUtil.toBean(form, CertifiedCheckStallParam.class);
        Boolean aBoolean =stallCheckService.stallCheck(param);
        return Result.getResult(Boolean.class).setMessage("审核成功").setData(aBoolean);
    }



    /**
     * update:出租客修改申请
     * @param authorization:token
     * @param form:出租客修改车位审核申请信息参数对象
     * @return
     * @throws Exception
     */
    @ApiOperation("出租客修改车位审核申请信息")
    @PostMapping("/update.renters")
    public Result<Boolean> update(@RequestBody CheckStallUpdateForm form,@RequestHeader String authorization)throws Exception{
        JwtUtil.getRentersId(authorization);
        CheckStallUpdateParam param = BeanUtil.toBean(form,CheckStallUpdateParam.class);
        Boolean aBoolean =stallCheckService.stallCheckUpdate(param);
        return Result.getResult(Boolean.class).setMessage("修改成功").setData(aBoolean);
    }

    /**
     * rentersSelect:出租客查询车位审核申请
     * @param authorization:token
     * @return
     * @throws Exception
     */
    @ApiOperation("出租客查询车位审核申请")
    @PostMapping("/rentersSelect.renters")
    public Result<RentersSelectStallCheckDTO> rentersSelect(@RequestHeader String authorization)throws Exception{
        Integer rentersId = JwtUtil.getRentersId(authorization);
        RentersSelectStallCheckDTO dto =stallCheckService.rentersSelect(rentersId);
        return Result.getResult(RentersSelectStallCheckDTO.class).setMessage("查询成功").setData(dto);
    }

    /**
     * certifiedSelect:物业查询申请
     * @param form 物业查询车位审核申请
     * @param authorization
     * @return
     * @throws Exception
     */
    @ApiOperation("物业查询车位审核申请")
    @PostMapping("/certifiedSelect.certified")
    public Result<PageInfo> certifiedSelect(@RequestBody CertifiedSelectCheckStallForm form,@RequestHeader String authorization)throws Exception{
        Integer certifiedAccountId = JwtUtil.getCertifiedAccountId(authorization);
        CertifiedSelectCheckStallParam param = BeanUtil.toBean(form, CertifiedSelectCheckStallParam.class);
        param.setCertifiedAccountId(certifiedAccountId);
        PageInfo pageInfo = stallCheckService.certifiedSelect(param);
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }

    /**
     * platformSelect ：平台查询车位审核申请
     * @param form  平台查询车位审核申请参数对象
     * @param authorization token
     * @return PageInfo 分页对象
     * @throws Exception
     */
    @ApiOperation("平台查询车位审核申请")
    @PostMapping("/platformSelect.platform")
    public Result<PageInfo> platformSelect(@RequestBody PlatformSelectCheckStallForm form,@RequestHeader String authorization)throws Exception{
        System.out.println("测试数据："+form);
        JwtUtil.getPlatformId(authorization);
        PlatformSelectCheckStallParam param = BeanUtil.toBean(form, PlatformSelectCheckStallParam.class);
        PageInfo pageInfo = stallCheckService.platformSelect(param);
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }


}

